/*!
 * 列表盒.
 * ecp.qzz.ui.listbox.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.panel","qzz.list","qzz.paged"], function() {
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 视图控件.
	 */
	qzz.ui.listbox = qzz.Class(qzz.ui.panel, {
		_create:function(){
		    this.base();
			this.getOption().rowHeight = this.getOption().rowHeight || 100;
			this.getOption().groupHeight = this.getOption().groupHeight || 40;
			this.getOption().autoHeight = this.getBooleanValue(this.getOption().autoHeight, true);
			this.getOption().autoWidth = this.getBooleanValue(this.getOption().autoWidth, true);			
			this.getOption().fieldList = this.getOption().fieldList || [];
			this.getOption().captionList = this.getOption().captionList || [];			
			this.getOption().desc = this.getBooleanValue(this.getOption().desc, true);
			this.getOption().pageSize = this._getNumberValue(this.getOption().pageSize, 10);
			this.getOption().pageIndex = this._getNumberValue(this.getOption().pageIndex, 1);
			if(this.getOption().pageSizeList == null) {			
			    this.getOption().pageSizeList = [10,25,50,100,200,this.getI18n("qzz.all", "全部")];			    
			}
			this._itemList = new qzz.list();
			if(this.isArray(this.getOption().dataList)) {
				this._itemList.setItemList(this.getOption().dataList);
				if(this.getOption().sort != null) {
					this._itemList.sort(this.getOption().sort, this.getOption().desc);
				}
				this.getOption().totalCount = this._itemList.length();
			}
			
		},
		_render:function(){
			this.base();
			this._boxTab = this.createElement("TABLE");
			this.setStyle(this._boxTab, "winStyle");
			this.thisWindow.appendChild(this._boxTab);	
			this.pageObj = new qzz.ui.paged({"totalCount":this.getOption().totalCount});
			this.pageObj.setPageSizeList(this.getOption().pageSizeList);
			var _this = this;
			this.pageObj.bind("onPageChanged", function(pc,ps,pi){
				_this.getOption().pageCount = pc;
				_this.getOption().pageSize = ps;
				_this.getOption().pageIndex = pi;
				_this.trigger("onPageChanged", [pc, ps, pi]);
				_this.refresh();
			});
			this.refresh();
		},
		_autoWidth:function() {
			if(this.getOption().autoWidth) {
				this.thisWindow.style.width = "100%";
			}
		},
		getWidth:function() {
			if(this.getOption().autoWidth) {
				return this.thisWindow.offsetWidth;
			} else {		
			    return this.base();
			}
		},
		_afterRender:function(){
			this.base();
		},
		_refreshBox:function(td, item, index) {
			if(this.hasbind("onFormat")) {
				var fmstr = this.trigger("onFormat", [td, item, this.getOption(), index]);
				if(fmstr != null) {
					if(typeof fmstr == "string") {
					    this._setHtml(td, fmstr, false);
					} else if(fmstr.length > 0) {
						td.appendChild(fmstr[0]);
					} else {
						td.appendChild(fmstr);
					}	
				}
			} else {
				//默认排布.
			    if(this.getOption().group != null) {
			    	this._setHtml(td, item[this.getOption().group], false);
			    }
			}
		},
		collapse:function(row) {
			var ri = row.rowIndex;
			var sumh = 0;
			for(var i = ri + 1, len = this._boxTab.rows.length; i < len; i++) {
				var ctr = this._boxTab.rows[i];
				var th = ctr.offsetHeight;
				if(this.getBooleanValue(ctr.getAttribute("isGroup"), false)) {
					break;
				} else {
					ctr.style.display = "none";
				}				
				if(th == 0) {
					th = this.getOption().rowHeight;
				}
				sumh += th;
			}
			if(this.getOption().autoHeight) {
			    this.thisWindow.style.height = "100%";
			} else {
				this.setHeight(this.getHeight() - sumh);
			}			
			row.isExpand = false;
		},
		expand:function(row) {
			var ri = row.rowIndex;
			var sumh = 0;
			for(var i = ri + 1, len = this._boxTab.rows.length; i < len; i++) {
				var ctr = this._boxTab.rows[i];
				if(this.getBooleanValue(ctr.getAttribute("isGroup"), false)) {
					break;
				} else {
					ctr.style.display = "";
				}
				var th = ctr.offsetHeight;
				if(th == 0) {
					th = this.getOption().rowHeight;
				}
				sumh += th;
			}
			if(this.getOption().autoHeight) {
			    this.thisWindow.style.height = "100%";
			} else {
				this.setHeight(this.getHeight() + sumh);
			}
			
			row.isExpand = true;
		},
		
	    refresh:function() {
			//this.base();
			this._boxTab.style.width = "100%";
			var sumh = 0;
			var _this = this;
			var groupValue = null, gv, mi = 0, tmpGroupTd = null, groupCount = 0,  
			    len = this._itemList.length();
			var i = 0;			
			if(this.getOption().pageSize === this.getI18n("qzz.all", "全部") || this.hasbind("onPageChanged")) {
				i = 0
			} else {
				i = (this.getOption().pageIndex - 1) * this.getOption().pageSize;
				len = this.getOption().pageSize + i;
				if(len > this._itemList.length()) {
					len = this._itemList.length();
				}				
			}
			var ti = 0;
			this.pageObj.setParent(this.thisWindow);
			for(; i < len; i++, ti++) {
				var tr = null, td = null;
				var citem = this._itemList.getItem(i);
				if(this.getOption().group != null) {
					gv = citem[this.getOption().group];
					if(gv != null && groupValue != gv) {
						if(ti + mi < this._boxTab.rows.length) {
							tr = this._boxTab.rows[ti + mi];
							tr.style.display = "";
							if(tr.cells.length > 0) {
								td = tr.cells[0];
								this._setHtml(td, "", false);
							} else {
								td = tr.insertCell(0);
							}					
						} else {
						    tr = this._boxTab.insertRow(ti + mi);
						    td = tr.insertCell(0);
						    this._setHtml(td, "", false);
						}
						td.onclick = null;
						tr.style.display = "";
						//创建子tab
						var ctab = this.createElement("TABLE");
						this.setStyle(ctab, "noborder");
						ctab.style.width = "100%";
						var ctr = ctab.insertRow(0);
						var ctda = ctr.insertCell(0);
						this.setStyle(ctda, "listbox_expand");
						this._setHtml(ctda, "-", false);
						td.expandtd = ctda;
						var ctdt = ctr.insertCell(1);
						ctdt.style.textAlign = "left";
						ctdt.style.border = "0px solid #cecece";
						td.texttd = ctdt;
						tr.isExpand = true;
						ctda.row = tr;
						ctda.onclick = function() {
							if(this.row.isExpand) {
							    _this.collapse(this.row);
							    _this._setHtml(this, "+", false);
							} else {
								_this.expand(this.row);
								_this._setHtml(this, "-", false);
							}
						}
						ctab.style.height = this.getOption().groupHeight + "px";
						td.appendChild(ctab);
						
						this.setStyle(td, "listbox_group");			
				        td.style.height = this.getOption().groupHeight + "px";
				        if(this.hasbind("onGroupFormat") !== true) {				        
				        	this._setHtml(td.texttd, gv, false);
				        }
				        tr.setAttribute("isGroup", true);
				        var th = td.offsetHeight;
						if(th == 0) {
							th = this.getOption().groupHeight;
						}
						sumh += th;
				        if(tmpGroupTd != null) {
				        	if(this.hasbind("onGroupFormat")) {
					        	var fmstr = this.trigger("onGroupFormat", [td.texttd, item, groupCount, this.getOption()]);
								if(typeof fmstr == "string") {
									this._setHtml(td.texttd, fmstr, false);
								} else if(fmstr.length > 0) {
									td.texttd.appendChild(fmstr[0]);
								} else {
									td.texttd.appendChild(fmstr);
								}
					        } else {
					        	this._setHtml(tmpGroupTd.texttd, tmpGroupTd.texttd.innerHTML + "&nbsp;&nbsp;|&nbsp;&nbsp;共计"+groupCount+"记录", false);
					        }
				        }				        
				        tmpGroupTd = td;
				        groupCount = 0;
				        groupValue = gv;
						mi ++;
					}
				}
				if(ti + mi < this._boxTab.rows.length) {
					tr = this._boxTab.rows[ti + mi];
					tr.style.display = "";
					if(tr.cells.length > 0) {
						td = tr.cells[0];
						td.style.textAlign = "";
						this._setHtml(td, "", false);
					} else {
						td = tr.insertCell(0);
					}
					
				} else {
				    tr = this._boxTab.insertRow(ti + mi);
				    td = tr.insertCell(0);
				    this._setHtml(td, "", false);
				}	
				td.onclick = function() {
					_this.trigger("onClick", [this.parentNode.item])
				}
				tr.item = citem; 
				tr.style.display = "";
				tr.setAttribute("isGroup", false);
				this.setStyle(td, "listbox");			
				td.style.height = this.getOption().rowHeight + "px";
				this._refreshBox(td, this._itemList.getItem(i), i);
				groupCount ++;
				var th = td.offsetHeight;
				if(th == 0) {
					th = this.getOption().rowHeight;
				}
				sumh += th;
			}
			//添加分页面板
			if(ti + mi < this._boxTab.rows.length) {
				tr = this._boxTab.rows[ti + mi];
				tr.style.display = "";
				if(tr.cells.length > 0) {
					td = tr.cells[0];
					this._setHtml(td, "", false);
				} else {
					td = tr.insertCell(0);
				}					
			} else {
			    tr = this._boxTab.insertRow(ti + mi);
			    td = tr.insertCell(0);
			}
			
			this.pageObj.setParent(td);
			tr.setAttribute("isGroup", true);
			td.style.height = this.getOption().groupHeight + "px";
			td.style.textAlign = "right";
			var th = td.offsetHeight;
			if(th == 0) {
				th = this.getOption().groupHeight;
			}
			sumh += th;
			td.style.paddingRight = "8px";
			this.setStyle(td, "listbox_group");	
			i++;
			
			
			while(this._boxTab.rows.length > ti + mi + 1) {
				this._boxTab.deleteRow(ti + mi + 1);
			}
			if(this.getOption().group != null) {
				if(tmpGroupTd != null) {
					if(this.hasbind("onGroupFormat")) {
			        	var fmstr = this.trigger("onGroupFormat", [td.texttd, item, groupCount, this.getOption()]);
						if(typeof fmstr == "string") {
							this._setHtml(td.texttd, fmstr, false);
						} else if(fmstr.length > 0) {
							td.texttd.appendChild(fmstr[0]);
						} else {
							td.texttd.appendChild(fmstr);
						}
			        } else {
		                this._setHtml(tmpGroupTd.texttd, tmpGroupTd.texttd.innerHTML + "&nbsp;&nbsp;|&nbsp;&nbsp;共计"+groupCount+"记录", false);
			        }
		        }	
			}
			if(this.getOption().autoHeight) {
			    //this.setHeight(sumh);
			    this.thisWindow.style.height = "100%";
			}
			this._autoWidth();
		},
		setSort:function(value) {
			if(value != null && value !== "" && this.getOption().sort != value) {
				this.getOption().sort = value;
				this._itemList.sort(this.getOption().sort, this.getOption().desc);
			}
		},
		setGroup:function(value) {
			if(value != null && value !== "" && this.getOption().group != value) {
				this.getOption().group = value;
			}
		},
		setTotalCount:function(value) {
			this.getOption().totalCount = parseInt(value, 10);
			this.pageObj.setTotalCount(this.getOption().totalCount);
		},
		/**
		 * 初始化数据.
		 * @params datas 数据列表.
		 */
		value:function(datas) {
			if(datas != null) {
				this.getOption().dataList = datas;
				if(this.isArray(this.getOption().dataList)) {
					this._itemList.setItemList(this.getOption().dataList);
					if(this.getOption().sort != null) {
						this._itemList.sort(this.getOption().sort, this.getOption().desc);
					}
					if(this.hasbind("onPageChanged") === false) {
						this.getOption().totalCount = this._itemList.length();
						this.pageObj.setTotalCount(this.getOption().totalCount);
					}
					this.refresh();
				}
			} else {
				return this.getOption().dataList;
			}
		}
	});
	/**
	 * 列表框控件.
	 */
	_bindRenderFunc("qzzlistbox", qzz.ui.listbox);
	return qzz.ui.listbox;
});
